from rest_framework.viewsets import ModelViewSet
from django.contrib.auth.models import Group
from meiduo_admin.utils.pagination import MeiduoPagination
from meiduo_admin.serializers.groups import GroupSerializer, GroupSimpleSerializer
from rest_framework.decorators import action
from rest_framework.response import Response


class GroupView(ModelViewSet):
    queryset = Group.objects.all()

    # serializer_class = GroupSerializer
    def get_serializer_class(self):
        # list===>GroupSerializer
        # simple====>GroupSimpleSerializer
        if self.action == 'list':
            return GroupSerializer
        elif self.action == 'simple':
            return GroupSimpleSerializer

    pagination_class = MeiduoPagination

    @action(methods=['get'], detail=False)
    def simple(self, request, *args, **kwargs):
        queryset = self.get_queryset()

        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)
